package com.mxpio.erp.sales.policy.impl;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.enums.OrderEnums;
import com.mxpio.erp.common.jpa.model.Order;
import com.mxpio.erp.common.order.policy.AbstractBizOrderPolicy;
import com.mxpio.erp.common.order.policy.OrderContext;
import com.mxpio.erp.common.order.service.OrderLineService;
import com.mxpio.erp.common.quality.entity.SoInQualityOrder;
import com.mxpio.erp.common.quality.entity.SoInQualityOrderLine;
import com.mxpio.erp.common.sales.entity.*;
import com.mxpio.erp.common.vo.NoticeClassVo;
import com.mxpio.erp.common.vo.OrderExcuteLineVo;
import com.mxpioframework.jpa.JpaUtil;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 销售订单执行策略
 * @author fangsong
 *
 */
public class DefaultShippingPolicy extends AbstractBizOrderPolicy {

	private List<OrderExcuteLineVo> orderExcuteVos;
	private OrderLineService orderLineService;
	public DefaultShippingPolicy(List<OrderExcuteLineVo> orderExcuteVos, OrderLineService orderLineService) {
		this.orderExcuteVos = orderExcuteVos;
		this.orderLineService = orderLineService;
	}

	@Override
	public void execute(OrderContext context) {
	}

	@Override
	public OrderContext config(OrderContext context) {
		context.setOrderExcuteVos(orderExcuteVos);
		context.setNoticeClass(ShippingNotice.class);
		context.setAccessType(OrderEnums.AccessType.OUT.getCode());
		context.setPickingType(NoticeStatusEnums.NoticeType.SO_OUT.getCode());
		return context;
	}

	@Override
	public boolean beforeExecute(OrderContext context) {
		//此处写生产订单执行前置校验逻辑
		Order order = context.getEntity();
		if (order instanceof SalesOrder && context.getOrderExcuteVos().size()>0){
			List<OrderExcuteLineVo> orderExcuteVos = context.getOrderExcuteVos();
			List<String> orderlinesStr = orderExcuteVos.stream().map(OrderExcuteLineVo::getBizOrderlineNo).collect(Collectors.toList());
			List<SalesOrderLine> orderLines = JpaUtil.linq(SalesOrderLine.class).equal("bizNo", order.getBizNo()).in("lineNo", orderlinesStr).list();

			NoticeClassVo shippingNoticeClazz = new NoticeClassVo(ShippingNotice.class,ShippingNoticeLine.class);
			NoticeClassVo rejectBillNoticeClazz = new NoticeClassVo(RejectBill.class, RejectBillLine.class);
			NoticeClassVo inQualityOrderClazz = new NoticeClassVo(SoInQualityOrder.class, SoInQualityOrderLine.class);//增加入库验收单 数据统计
			List<NoticeClassVo> noticeClassVos = new ArrayList<NoticeClassVo>();
			noticeClassVos.add(shippingNoticeClazz);
			noticeClassVos.add(rejectBillNoticeClazz);
			noticeClassVos.add(inQualityOrderClazz);
			orderLineService.handleBizOrderLineQuantity(orderLines, noticeClassVos, OrderEnums.AccessType.OUT.getCode());
			Map<String, SalesOrderLine> orderLineMap = JpaUtil.index(orderLines,"lineNo");
			for(OrderExcuteLineVo orderExcuteVo : orderExcuteVos) {
				BigDecimal lineQuantity = orderExcuteVo.getLineQuantity();
				BigDecimal quantity = orderExcuteVo.getQuantity();
				String bizOrderlineNo = orderExcuteVo.getBizOrderlineNo();
				SalesOrderLine orderLine = orderLineMap.get(bizOrderlineNo);
				if (orderLine!=null){
					BigDecimal actualQuantity = orderLine.getActualQuantity();
					BigDecimal actualRejectQuantity = orderLine.getActualRejectQuantity();
					BigDecimal planQuantity = orderLine.getPlanQuantity();
					//最大可发数量：执行行总数量减去实际已发，减去计划发货，加上已退数量，不考虑计划退货数量
					BigDecimal lastMax = lineQuantity.subtract(actualQuantity).subtract(planQuantity).add(actualRejectQuantity);
					if (quantity.compareTo(lastMax)>0){
						context.addError("提交数据中行号【"+bizOrderlineNo+"】执行数量大于最大可执行数量【"+lastMax+"】。");
						return false;
					}
				}
			}
		}
		return true;
	}

	@Override
	public void afterExecute(OrderContext context) {
		/*Order order = context.getEntity();//订单完成更新放到对应的仓单执行逻辑
		if(!(order instanceof SalesOrder)){
			return;
		}
		List<SalesOrderLine> salesOrderLineList = JpaUtil.linq(SalesOrderLine.class).equal("bizNo",order.getBizNo()).list();
		if(CollectionUtils.isEmpty(salesOrderLineList)){
			return;
		}


		String status = OrderStatusEnums.OrderStatus.FINISHED.getStatusCode();
		for(SalesOrderLine salesOrderLine:salesOrderLineList){
			List<ShippingNotice> noticeList = JpaUtil.linq(ShippingNotice.class).equal("bizOrderNo", salesOrderLine.getBizNo()).list();
			if(CollectionUtils.isNotEmpty(noticeList)){
				Set<String> noticeNoSet = noticeList.stream().map(ShippingNotice::getNoticeNo).collect(Collectors.toSet());
				List<ShippingNoticeLine> noticeLineList = JpaUtil.linq(ShippingNoticeLine.class).in("noticeNo",noticeNoSet).equal("itemCode",salesOrderLine.getItemCode()).list();
				BigDecimal planQuantity = noticeLineList.stream().map(ShippingNoticeLine::getQuantity).reduce(BigDecimal.ZERO,BigDecimal::add);
				if(planQuantity.compareTo(salesOrderLine.getQuantity())<0){
					status = OrderStatusEnums.OrderStatus.PROCESSING.getStatusCode();
					break;
				}
			}
			else{
				status = OrderStatusEnums.OrderStatus.PROCESSING.getStatusCode();
			}

		}
		order.setOrderStatus(status);
		order.setExecuteTime(new Date());
		JpaUtil.update(order);*/


	}

}
